<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/page/common/commonConfig.jsp"%>
<%--
传入本页面可以带有参数约束选择单位信息
max(max=数量) 约束最多选择的单位数量，默认不限制数量
unitIds(unitIds=id1,id2,id3) 默认已经选择的单位，传入以,分割的字符串，默认无

父页面应该包含以下函数，用来本页面执行确定后调用。
getChooseUnitResult(ids,names,newaddIds,newaddNames,deletedIds);
ids是选中的全部id
names是选中的全部name
newaddIds是在与传入本页参数unitIds时配合使用，当存在unitIds时候，在基于unitIds基础之上新增的全部id
newaddNames配合newaddIds的所有name
deletedIds是在与传入本页参数unitIds时配合使用，当存在unitIds时候，删除了unitIds中所有id
 --%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>选择单位页面</title>
   	<script src="<%=basePath%>component/layout.js"></script>
   	<script src="<%=basePath%>component/tree.js"></script>
   	<script src="<%=basePath%>component/grid.js"></script>
   	<script src="<%=basePath%>component/toolbar.js"></script>
   	<script type="text/javascript">
var dhxLayout = createLayoutNormal('2U');
var unitlistGriddone = null;    	
function configLayout(){
	var a = dhxLayout.cells('a');
	var b = dhxLayout.cells('b');
	a.setText('检索区');
	b.setText('已选择区');
	a.setWidth(300);
	a.fixSize(true, true);
	b.fixSize(true, true);
	b.hideHeader();
	initTree(dhxLayout,'a');
	createToolbarFromDHXLayoutCell(b,operateToolbarChooseDone);
};
var dhxGridUnitListChooseDone= {
	initDataTable : function (layout,cell) {
		unitlistGriddone = createGridFromLayout(layout,cell);
	   	unitlistGriddone.setHeader("序号,上级单位名称,单位名称");
		unitlistGriddone.setInitWidths("40,*,200");
		unitlistGriddone.setColAlign("right,right,left");
		unitlistGriddone.setColSorting("int,str,str");
		unitlistGriddone.setColTypes("ro,ro,ro");
		unitlistGriddone.selMultiRows = true;
		unitlistGriddone.init();
		setSkin(unitlistGriddone);
		if('${empty param.unitIds}' == 'false'){
			layoutProgressOn(dhxLayout);
			unitlistGriddone.loadXML('<%=basePath%>system-chooseComponent/ChooseUnitAction!queryUnitInfoByUnitIds.action?unitIds=${param.unitIds}',
			function(){
				unitlistGriddone.forEachRow(function(id){
					var supername = getAllParentItemText(id);
					unitlistGriddone.cells(id,1).setValue(supername);
				 });
				layoutProgressOff(dhxLayout);
			});
		}
	}
	,addItem : function(id,supername,name){
		unitlistGriddone.addRow(id, [''+(unitlistGriddone.getRowsNum()+1),supername,name], ''+unitlistGriddone.getRowsNum());
	}
	,selectAll : function(){
		unitlistGriddone.selectAll();
	}
	,clearAll : function(){
		unitlistGriddone.clearAll();
	}
};
function operateToolbarChooseDone(toolbar){
    toolbar.addText("info", 1, "已选择区");
    toolbar.addSeparator("sep1", 2);
    toolbar.addButton("undo", 3, "取消选择", "undo.gif", "undo_dis.gif");
    toolbar.addButton("selectAll", 4, "全选", "selection.gif", "selection.gif");
    toolbar.addButton("saveAll", 5, "确定", "save.gif", "save_dis.gif");
    if('${empty param.max}' == 'false'){
    	toolbar.addText("infotip", 6, ('最多选择 [ ${param.max} ]个单位'));
	}
    toolbar.attachEvent("onClick", function(id) {
    	if(id == 'undo'){
    		var selids = unitlistGriddone.getSelectedId();
    		if(selids != null){
    			var ids = selids.split(',');
    			for(var i = 0;i<ids.length;i++){
					var lastrowIndex = unitlistGriddone.getRowIndex(ids[i])-1;
					unitlistGriddone.deleteSelectedRows();
					unitlistGriddone.selectRow(lastrowIndex);
					for(var i=(lastrowIndex+1) ;i<(unitlistGriddone.getRowsNum());i++){
						unitlistGriddone.cells(unitlistGriddone.getRowId(''+i),0).setValue(i+1);
					}
				}
			}
    	}
    	if(id == 'selectAll'){
    		dhxGridUnitListChooseDone.selectAll();
    	}
    	if(id == 'saveAll'){
    		setParentResult();
    	}
    });
};

function initTree(tabbar,cell){
	MyTree.initMyTreeFromLayout(tabbar,cell);
	MyTree.setupTree("<%=basePath%>system-unit/UnitAction!unitTree.action",function(id){
		if('${empty param.max}' == 'false'){
			if(unitlistGriddone.getRowsNum() >= '${param.max}'){
				return ;
			}
		}
		var supername = getAllParentItemText(id);
		if(unitlistGriddone.getAllRowIds(',').indexOf(id)<0){
			dhxGridUnitListChooseDone.addItem(id,supername,MyTree.tree.getItemText(id));
		};
		unitlistGriddone.selectRowById(id);
	},function(){
		dhxGridUnitListChooseDone.initDataTable(dhxLayout,"b");
	});
};

function getAllParentItemText(itemId){
	var sid = MyTree.tree.getParentId(itemId);
	var supername = '';
	for(var i=0;i == 0;){
		if(sid !=0){
			supername = (MyTree.tree.getItemText(sid) + (supername =='' ?'':',') + supername);
			sid = MyTree.tree.getParentId(sid);
			continue;
		}
		if(sid == 0 ){
			break;
		}
	}
	return supername;
}

function setParentResult(){
	var ids = unitlistGriddone.getAllRowIds(',');
	var names = '';
	if(ids != ''){
		var idslist = ids.split(',');
		for(var i=0;i<idslist.length;i++){
			names += ( (names==''?'':',')+ unitlistGriddone.cells(idslist[i],2).getValue());
		}
	}
	var newaddIds = ids;
	var deletedIds = '';
	if('${empty param.unitIds}' == 'false'){
		var uids = ('${param.unitIds}').split(',');
		for(var i = 0;i<uids.length;i++){
			var uid = uids[i];
			if(newaddIds.indexOf(uid) == 0){
				newaddIds = newaddIds.replace((uid+','),'');
				newaddIds = newaddIds.replace((uid),'');
				continue;
			}
			if (newaddIds.indexOf(uid) < 0){
				if(deletedIds == ''){
					deletedIds += uid;
				}else{
					deletedIds +=(','+uid);
				}
				continue;
			}
			if (newaddIds.indexOf(uid) > 0){
				newaddIds = newaddIds.replace((','+uid),'');
				newaddIds = newaddIds.replace((uid),'');
				continue;
			}
		}
	}
	var newaddNames = names;
	if(newaddIds != ids && newaddIds != ''){
		var newaddNames = '';
		var nids = newaddIds.split(',');
		for(var i=0;i<nids.length;i++){
			var nid = nids[i];
			newaddNames += ( (newaddNames==''?'':',')+ unitlistGriddone.cells(nids[i],2).getValue());
		}
	}
	if(window.parent.getChooseUnitResult){
		layoutProgressOn(dhxLayout);
		window.parent.getChooseUnitResult(ids,names,newaddIds,newaddNames,deletedIds);
	}
}
$(function (){
	configLayout();
});
   	</script>
  	
  </head>
  
<body >
</body>
</html>